﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using AsyncCamera.Camera.Structure;

namespace AsyncCamera.Camera.media
{
    public abstract class MediaSink: MediaElement
    {
        protected Dictionary<Guid, MediaSource> plugIn;
        protected Dictionary<Guid, Frame> lastFrame;
        protected long sleepTime = 100;
        protected long lastTime;

        public MediaSource this[Guid i]
        {
            get
            {
                return plugIn[i];
            }
        }

        public static MediaSink operator +(MediaSink container, MediaSource newPlugIn)
        {
            container.plugSource(newPlugIn);
            return container;
        }

        public virtual void plugSource(MediaSource newPlugIn)
        {
            plugIn.Add(newPlugIn.GUID, newPlugIn);
            lastFrame.Add(newPlugIn.GUID, newPlugIn.Frame);
        }

        public MediaSink(): base()
        {
            Initialize();
        }

        protected override void Initialize()
        {
            sleepTime = 100;
            lastFrame = null;
            lastTime = 0;
            plugIn = new Dictionary<Guid, MediaSource>();
            lastFrame = new Dictionary<Guid, Frame>();
            currentState = MediaState.Stop;
        }

        public override void Start()
        {
            switch (currentState)
            {
                case MediaState.Stop:
                    metronome = new DispatcherTimer();
                    metronome.Interval = new TimeSpan(sleepTime);
                    metronome.Tick += new EventHandler(MultiTick);
                    metronome.Start();
                    currentState = MediaState.Play;
                    break;
                case MediaState.Pause:
                    metronome.Stop();
                    currentState = MediaState.Play;
                    break;
            }
        }

        public override void Stop()
        {
            switch (currentState)
            {
                case MediaState.Play:
                    metronome.Stop();
                    metronome = null;
                    currentState = MediaState.Stop;
                    break;
                case MediaState.Pause:
                    metronome = null;
                    currentState = MediaState.Stop;
                    break;
            }
        }

        public override void Pause()
        {
            switch (currentState)
            {
                case MediaState.Play:
                    metronome.Stop();
                    currentState = MediaState.Pause;
                    break;
            }
        }

        protected void MultiTick(object sender, EventArgs e)
        {
            long currentTime = DateTime.Now.Ticks;
            if ((currentTime - lastTime) >= (10000000 / fps))
            {
                lastTime = currentTime;
                Tick(sender, e);
            }
        }
    }
}
